package com.kim.rssreader.utils;

import java.io.IOException;
import java.io.InputStream;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.drawable.Drawable;

import com.kim.rssreader.service.StarterImageFetchService.ImageConfFetchCompleteCallback;
import com.kim.rssreader.service.StarterImageFetchService.ImageDataFecthCompleteCallback;
import com.kim.rssreader.utils.DrawableUtils.ImageSaveCallback;
import com.kim.rssreader.utils.NetUtil.NetProcessComplete;

public class StarterImageFetchUtils {

	/**
	 * 图片配置文件名称
	 */
	private static final String START_IMAGE_VERSION_CONFIG = "start_image_version_config";

	private static final String START_IMAGE_VERSION_KEY = "iv";

	private static final String START_IMAGE_URL_KEY = "iu";

	private static final String START_IMAGE_URL_SPLIT = "\\;";

	public static final void fetchImage(final Context context,
			final ImageConfFetchCompleteCallback imageConfFetchCompleteCallback) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				fetchImageVersion(context, imageConfFetchCompleteCallback);
			}
		}).start();
	}

	/**
	 * 获取图片地址
	 * 
	 * @param context
	 */
	public static final void fetchImageVersion(final Context context,
			final ImageConfFetchCompleteCallback imageConfFetchCompleteCallback) {
		NetUtil.getInputStream(ValueConstant.START_IMAGE_CONFIG_FILE_URL,
				new NetProcessComplete() {
					@Override
					public void onProcess(InputStream inputStream) {
						try {
							String icjson = NetUtil
									.convertInputStream2String(inputStream);
							JSONObject jsonObject = new JSONObject(icjson);
							int newVersion = jsonObject
									.getInt(START_IMAGE_VERSION_KEY);
							int localVersion = getLocalImageVersion(context);
							if (localVersion >= newVersion) {
								imageConfFetchCompleteCallback
										.onImageConfComplete();
								return;
							}
							updateLocalImageVersion(context, newVersion);
							String imageUrls = jsonObject.getString("iu");
							setImageUrlByVersion(context, newVersion, imageUrls);
							imageConfFetchCompleteCallback
									.onImageConfComplete();
						} catch (IOException e) {
							e.printStackTrace();
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
	}

	/**
	 * 获取图片数据
	 * 
	 * @param context
	 */
	public static final void fetchImageData(Context context, int targetVersion,
			ImageDataFecthCompleteCallback imageDataFecthCompleteCallback) {
		String imageUrls = getImageUrlByVersion(context, targetVersion);
		if (imageUrls == null || "".equals(imageUrls)) {
			imageDataFecthCompleteCallback.onImageDataComplete();
			return;
		}
		String[] imageUrlArray = imageUrls.split(START_IMAGE_URL_SPLIT);
		if (imageUrlArray == null || imageUrlArray.length == 0) {
			imageDataFecthCompleteCallback.onImageDataComplete();
			return;
		}
		for (String imageUrl : imageUrlArray) {
			DrawableUtils.saveImage(imageUrl, new ImageSaveCallback() {

				@Override
				public void onSuccess(String localPath) {

				}

				@Override
				public void onError(String errorMsg) {

				}
			});
		}
		imageDataFecthCompleteCallback.onImageDataComplete();
	}

	/**
	 * 加载图片
	 * 
	 * @param context
	 * @param targetVersion
	 * @return
	 */
	public static final Drawable[] loadImage(Context context, int targetVersion) {
		if (targetVersion <= 0) {
			return null;
		}
		String imageUrls = getImageUrlByVersion(context, targetVersion);
		if (imageUrls == null || "".equals(imageUrls)) {
			return loadImage(context, targetVersion - 1);
		}
		String[] imageUrlArray = imageUrls.split(START_IMAGE_URL_SPLIT);
		if (imageUrlArray == null || imageUrlArray.length == 0) {
			return loadImage(context, targetVersion - 1);
		}
		int index = 0;
		Drawable[] drawables = new Drawable[imageUrlArray.length];
		for (String imageUrl : imageUrlArray) {
			Drawable drawable = DrawableUtils.loadDrawableByNetUrl(imageUrl);
			if (drawable == null) {
				return loadImage(context, targetVersion - 1);
			}
			drawables[index] = drawable;
			index++;
		}
		deleteOldVersionImage(context, targetVersion - 1);
		return drawables;
	}

	/**
	 * 删除历史图片
	 * 
	 * @param context
	 * @param oldVersion
	 */
	public static final void deleteOldVersionImage(Context context,
			int oldVersion) {
		if (oldVersion < 1) {
			return;
		}
		String imageUrls = getImageUrlByVersion(context, oldVersion);
		if (imageUrls == null || "".equals(imageUrls)) {
			return;
		}
		String[] imageUrlArray = imageUrls.split(START_IMAGE_URL_SPLIT);
		if (imageUrlArray == null || imageUrlArray.length == 0) {
			return;
		}
		for (String imageUrl : imageUrlArray) {
			DrawableUtils.deleteDrawableByNetUrl(imageUrl);
		}
	}

	/**
	 * 获取本地图片版本号
	 * 
	 * @param context
	 * @return
	 */
	public static final int getLocalImageVersion(Context context) {
		return SharedPreferencesUtils.getValueInt(START_IMAGE_VERSION_KEY,
				START_IMAGE_VERSION_CONFIG, context);
	}

	/**
	 * 更新本地图片版本号
	 * 
	 * @param context
	 * @param newVersion
	 */
	public static final void updateLocalImageVersion(Context context,
			int newVersion) {
		SharedPreferencesUtils.setValue(START_IMAGE_VERSION_KEY, newVersion,
				START_IMAGE_VERSION_CONFIG, context);
	}

	/**
	 * 
	 * @param context
	 * @param version
	 * @return
	 */
	public static final String getImageUrlByVersion(Context context, int version) {
		return SharedPreferencesUtils.getValueString(START_IMAGE_URL_KEY
				+ version, START_IMAGE_VERSION_CONFIG, context);
	}

	/**
	 * 
	 * @param context
	 * @param version
	 * @param imageUrls
	 */
	public static final void setImageUrlByVersion(Context context, int version,
			String imageUrls) {
		SharedPreferencesUtils.setValue(START_IMAGE_URL_KEY + version,
				imageUrls, START_IMAGE_VERSION_CONFIG, context);
	}

}
